﻿@{
    ViewBag.Title = "Common Command";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
       <ol class="breadcrumb">
    <li><a href="/Home.html">Home</a></li>
    <li><a href="/WebApp/WebApi"> Sanelib WebApp</a></li>
    <li class="active">File Chunck Upload</li>
</ol>
       
            <div id="introduction">
                <h1 class="text-left">File Chunk Upload</h1>
                <h4>namespace : sanelib</h4>
            </div>
            <div id="start">
                <p>Class Inherited: <a href="/WebApp/SmartApi">SmartApiController</a></p>
                <table class=" table-condensed table-striped">
                    <tr>
                        <th><strong>Constructor Parameters</strong></th>
                    </tr>
                    <tr>
                        <td class="table-bordered">IUserSession,</td>
                        <td class="table-bordered">IMapingEngine,</td>
                    </tr>
                </table>
                <h4>Method : Post</h4>
                <article>
                    Description: Its Constructor contains two arguments which are of type int, Id and the FileIndex
                    <code>HttpContext.Current.Request.Files["Slice"];</code><br />
                    Gets the HttpRequest object for the current HTTP request.
                    <code>request.InputStream.Read(chunk, 0, Convert.ToInt32(request.ContentLength));</code><br /><br />
                    The Read Method of the Inputstream class will read the stream and will convert every chunk into the 32-bit Int. But the HttpContext Class is an helper class which helps in converting a file into chunks based on context and session.
                    <code>Request.Properties["MS_HttpContext"] as HttpContext;</code><br />
                    Now if Context and Session is available then an function named UploadCurrentChunk is called with the arguments model, chunk and id. if that call is successfullly retuned we either Commit or return same Data.
                </article>
                <h4>Method: UploadCurrentChunk</h4>
                <article>
                    Arguments Contains FileDetail of Model, chunk in Byte and Id.
                    <pre>Code Analysis<code>CreateFolderIfNeeded(model.FilePath + id + ".temp");
 var fileStream = new FileStream(model.FilePath + "//" + id + ".temp", FileMode.OpenOrCreate,
 FileAccess.ReadWrite);
 fileStream.Seek(fileStream.Length, SeekOrigin.Begin);
 fileStream.Write(chunk, 0, chunk.Length);
 fileStream.Close();
 return null;</code></pre>
                    This function reads the stream and write them in special hierarchy if available. No HttpResponse is returned.
                    An HttpExecption is usedd to catch exeception.
                </article>
                <h4>Method : CommitAllChunks</h4>
                <article>
                    Arguments contains FileDetail, who's object is used to verify wheather the upload is completed or not, its path, counts
                    <pre><code>
if (File.Exists(Globals.DownloadFolder + model.FileName))
{
File.Delete(Globals.DownloadFolder + model.FileName);
File.Move(model.FilePath + model.FileName, Globals.DownloadFolder + model.FileName);
}
else
{
File.Move(model.FilePath + model.FileName, Globals.DownloadFolder + model.FileName);
}
Directory.Delete(Globals.DownloadFolder + model.FileId, true);
</code></pre>
                    The above code shown is to move, delete files between directory. After that an HashCode is created with MD5 and an status message is saved with the operation on uploading the file.
                    <pre><code>
var md5 = new MD5CryptoServiceProvider())
{
var buffer = md5.ComputeHash(File.ReadAllBytes(filename));
return ToHex(buffer, false);
}
</code></pre>
                </article>
                <p>Finally an command is made with new Id of the model UpdateReceiveFileEndTIme and is excuted then the context class removes the current file and with its FileIndx</p>
                <h4>Converting to Hex</h4>
                <pre><code>
public static string ToHex(byte[] bytes, bool upperCase)
{
    var result = new StringBuilder(bytes.Length * 2);
       foreach (var t in bytes)
       result.Append(t.ToString(upperCase ? "X2" : "x2"));
       return result.ToString();
}
</code>An StringBilder is used to define size then an loop performs the Hex Formation accroding to the size of thr byte.</pre>
            </div>
      